﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// $Author:$
// $Id: TimeUtils.cs 20 2011-10-17 08:26:33Z masroore@gmail.com $
// $Rev:$
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;

namespace MaxTrader.IntradayQuotes.Core
{
    public static class TimeUtils
    {
        /// <summary>
        ///   <para>Truncates a DateTime to a specified resolution.</para>
        ///   <para>A convenient source for resolution is TimeSpan.TicksPerXXXX constants.</para>
        /// </summary>
        /// <param name = "date">The DateTime object to truncate</param>
        /// <param name = "resolution">e.g. to round to nearest second, TimeSpan.TicksPerSecond</param>
        /// <returns>Truncated DateTime</returns>
        public static DateTime Truncate(this DateTime date, long resolution)
        {
            return new DateTime(date.Ticks - (date.Ticks%resolution), date.Kind);
        }

        public static DateTime TruncateMilliSeconds(DateTime date)
        {
            return Truncate(date, TimeSpan.TicksPerSecond);
        }

        public static DateTime TruncateSeconds(this DateTime date)
        {
            var result = Truncate(date, TimeSpan.TicksPerMinute);
            result = Truncate(result, TimeSpan.TicksPerSecond);
            return result;
        }

        public static DateTime TruncateMinutes(this DateTime date)
        {
            var result = Truncate(date, TimeSpan.TicksPerHour);
            result = Truncate(result, TimeSpan.TicksPerMinute);
            result = Truncate(result, TimeSpan.TicksPerSecond);
            return result;
        }

        public static TimeSpan Round(this TimeSpan time, TimeSpan roundingInterval, MidpointRounding roundingType)
        {
            return new TimeSpan(
                Convert.ToInt64(Math.Round(
                    time.Ticks/(decimal) roundingInterval.Ticks,
                    roundingType
                                    ))*roundingInterval.Ticks
                );
        }

        public static TimeSpan Round(this TimeSpan time, TimeSpan roundingInterval)
        {
            return Round(time, roundingInterval, MidpointRounding.ToEven);
        }

        public static DateTime RoundDate(this DateTime datetime, TimeSpan roundingInterval)
        {
            return new DateTime((datetime - DateTime.MinValue).Round(roundingInterval).Ticks);
        }

        public static DateTime Round(this DateTime date, TimeSpan span)
        {
            long ticks = (date.Ticks + (span.Ticks/2) + 1)/span.Ticks;
            return new DateTime(ticks*span.Ticks);
        }

        public static DateTime RoundSeconds(this DateTime date, int seconds)
        {
            return date.Round(TimeSpan.FromSeconds(seconds));
        }

        public static DateTime RoundMinutes(this DateTime date, int minutes)
        {
            return date.Ceil(TimeSpan.FromMinutes(minutes));
        }

        public static DateTime RoundHours(this DateTime date, int hours)
        {
            return date.Round(TimeSpan.FromHours(hours));
        }

        public static DateTime Floor(this DateTime date, TimeSpan span)
        {
            long ticks = (date.Ticks/span.Ticks);
            return new DateTime(ticks*span.Ticks);
        }

        public static DateTime Ceil(this DateTime date, TimeSpan span)
        {
            long ticks = (date.Ticks + span.Ticks - 1)/span.Ticks;
            return new DateTime(ticks*span.Ticks);
        }
    }
}